﻿Imports System
Imports System.Collections.Generic
Imports System.Collections

Public Class Form1

    Dim al As ArrayList = New ArrayList()
    Dim mySL = New SortedList()
    Dim nextVersionFile As String = Nothing
    Dim dynal As ArrayList = New ArrayList()
    Dim sameClick_7 As Boolean = False

    Private Sub ProvideSourceList()
        al.Clear()
        mySL = New SortedList()
        'al.Add("A_0 Construction or Renovation_v01.docx")

        'al.Add("F_0_Department of Housing Preservation and Development Project_v01.xlsx")
        al.Add("B_0 Moveable Property, Initial Outfitting_v01.doc")
        al.Add("X_B_0 Moveable Property, Initial Outfitting_v02.doc")
        If (dynal.Count > 0) Then
            For Each s As String In dynal
                al.Add(s)
            Next
        End If
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        Dim sol As String = ""
        If (CheckBox1.Enabled) Then
            sol = "X_"
        Else

        End If

        If Not (sameClick_7) Then
            dynal.Clear()
        End If
        Label3.Text = String.Empty
        ProvideSourceList()

        nextVersionFile = GetNextAttachmentsSameFileName("B_0 Moveable Property, Initial Outfitting", ".doc", CheckBox1.Checked, sol, "_v")
        Label3.Text = nextVersionFile
        dynal.Add(nextVersionFile)
        LoadListViews()
        sameClick_7 = True
    End Sub
    Public Function GetNextAttachmentsSameFileName(ByVal leafName As String, ByVal ext As String) As String
        Dim alSame As ArrayList = New ArrayList()
        mySL = New SortedList()
        'Dim i As Integer = 0
        Dim justVersionNumber As Integer = 0
        For Each similar As String In al
            If (similar.ToLower().Contains(leafName.ToLower())) Then
                If (similar.IndexOf("_v_") > 0) Then
                    Dim lastPart As String = similar.Substring(similar.IndexOf("_v_"))

                    Dim extpos As Integer = lastPart.LastIndexOf(".")
                    lastPart = lastPart.Substring(0, extpos)

                    justVersionNumber = Convert.ToInt32(lastPart.Replace("_v_", ""))
                Else
                    justVersionNumber = 0
                End If
                alSame.Add(similar)
                mySL.Add(justVersionNumber, similar)
            End If
        Next

        ' Get the last instance from Sorted List
        Dim maxVersion = mySL.Count
        Dim leafFileName As String = String.Empty
        If (maxVersion.ToString().Length = 2) Then
            leafFileName = leafName + "_v_" + maxVersion.ToString()
        Else
            leafFileName = leafName + "_v_0" + maxVersion.ToString()

        End If
        leafFileName = leafFileName + ext
        Return leafFileName
    End Function

    Public Function GetNextAttachmentsSameFileName(ByVal leafName As String, ByVal ext As String, ByVal unSolicit As Boolean, ByVal preFix As String, ByVal suffix As String) As String
        If (unSolicit) Then
            If (Not leafName.StartsWith(preFix)) Then
                leafName = preFix + leafName
            End If
        End If
        Dim alSame As ArrayList = New ArrayList()
        mySL = New SortedList()
        Dim i As Integer = 0
        Dim justVersionNumber As Integer = 0
        For Each similar As String In al
            If (unSolicit) Then
                If (similar.Substring(0, similar.LastIndexOf(".")).Replace(preFix, "").ToLower().Contains(leafName.Replace(preFix, "").ToLower())) Then
                    Dim lastPart As String = String.Empty
                    If (similar.StartsWith(preFix)) Then
                        lastPart = similar.Substring(similar.IndexOf(preFix))
                        If (lastPart.IndexOf(suffix) > 0) Then
                            lastPart = lastPart.Substring(similar.IndexOf(suffix))

                            Dim extpos As Integer = lastPart.LastIndexOf(".")
                            lastPart = lastPart.Substring(0, extpos)

                            justVersionNumber = Convert.ToInt32(lastPart.Replace(suffix, ""))
                            alSame.Add(similar)
                            mySL.Add(i, similar)
                        End If
                    Else
                        If (similar.IndexOf(suffix) > 0) Then
                            lastPart = similar.Substring(similar.IndexOf(suffix))

                            Dim extpos As Integer = lastPart.LastIndexOf(".")
                            lastPart = lastPart.Substring(0, extpos)

                            justVersionNumber = Convert.ToInt32(lastPart.Replace(suffix, ""))
                            alSame.Add(similar)
                            mySL.Add(i, similar)
                        End If
                    End If
                End If
            ElseIf (similar.Substring(0, similar.LastIndexOf(".")).ToLower().Contains(leafName.ToLower())) Then
                If (similar.IndexOf(suffix) > 0) Then
                    Dim lastPart As String = similar.Substring(similar.IndexOf(suffix))

                    Dim extpos As Integer = lastPart.LastIndexOf(".")
                    lastPart = lastPart.Substring(0, extpos)

                    justVersionNumber = Convert.ToInt32(lastPart.Replace(suffix, ""))
                    alSame.Add(similar)
                    mySL.Add(i, similar)
                End If
            End If
            i = i + 1
        Next

        Dim maxVersion = 0
        ' Get the last instance from Sorted List
        maxVersion = justVersionNumber + 1
        Dim leafFileName As String = String.Empty
        If (maxVersion.ToString().Length >= 2) Then
            leafFileName = leafName + suffix + maxVersion.ToString()
        Else
            leafFileName = leafName + suffix + "0" + maxVersion.ToString()
        End If
        leafFileName = leafFileName + ext
        Return leafFileName
    End Function

    Private Sub LoadListViews()
        ListBox1.Items.Clear()
        ListBox2.Items.Clear()
        For Each s As String In al
            ListBox1.Items.Add(s)
        Next
        For Each sl As DictionaryEntry In mySL
            ListBox2.Items.Add(sl.Value)
        Next
    End Sub

End Class
